package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.InspectRegisterInfoMapper;
import cc.rengu.igas.bomp.common.entity.InspectRegisterInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 */
public class InspectRegisterInfoMapperImpl implements InspectRegisterInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertInspectRegisterInfo(InspectRegisterInfo inspectRegisterInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertInspectRegisterInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectRegisterInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检采集信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", inspectRegisterInfo.getInstId(), inspectRegisterInfo.getInspectTaskNo(), inspectRegisterInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateInspectRegisterInfoById(InspectRegisterInfo inspectRegisterInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateInspectRegisterInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectRegisterInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检采集信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", inspectRegisterInfo.getInstId(), inspectRegisterInfo.getInspectTaskNo(), inspectRegisterInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public InspectRegisterInfo selectInspectRegisterInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectRegisterInfoById");
        InspectRegisterInfo inspectRegisterInfo = new InspectRegisterInfo();
        inspectRegisterInfo.setInstId(instId);
        inspectRegisterInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectRegisterInfo, inspectRegisterInfo);
        if (0 != iReturnCode) {
            rglog.error("获取巡检采集信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return inspectRegisterInfo;
    }

    @Override
    public InspectRegisterInfo selectInspectRegisterInfoByPrimaryKey(String instId, String inspectTaskNo, String mchntNo, String collectId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectRegisterInfoByPrimaryKey");
        InspectRegisterInfo inspectRegisterInfo = new InspectRegisterInfo();
        inspectRegisterInfo.setInstId(instId);
        inspectRegisterInfo.setInspectTaskNo(inspectTaskNo);
        inspectRegisterInfo.setMchntNo(mchntNo);
        inspectRegisterInfo.setCollectId(collectId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectRegisterInfo, inspectRegisterInfo);
        if (0 != iReturnCode) {
            rglog.error("获取巡检采集信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,collectId:<{}>,iReturnCode:<{}>", instId, inspectTaskNo, mchntNo, collectId, iReturnCode);
            return null;
        }
        return inspectRegisterInfo;
    }

    @Override
    public List<InspectRegisterInfo> selectInspectRegisterInfoByMchntNo(String instId, String inspectTaskNo, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectRegisterInfoByMchntNo");
        InspectRegisterInfo inspectRegisterInfo = new InspectRegisterInfo();
        inspectRegisterInfo.setInstId(instId);
        inspectRegisterInfo.setInspectTaskNo(inspectTaskNo);
        inspectRegisterInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectRegisterInfo, InspectRegisterInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取巡检采集信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>", instId, inspectTaskNo, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (InspectRegisterInfo) item).collect(Collectors.toList());
    }
}
